//
//  WSJFindViewController.m
//  RunningMan
//
//  Created by dllo on 16/5/17.
//  Copyright © 2016年 WSJ. All rights reserved.
//

#import "WSJFindViewController.h"

#import "WSMyTool.h"
#import "WSAFNetWorkingTool.h"
#import "WSVideoTableViewCell.h"
#import "WSVideoModel.h"
#import "WSPlayVideoViewController.h"
#define URL @"http://api.wdquan.cn/moments?limit=10&page=1&section=19&cursor="

@interface WSJFindViewController ()<UITableViewDataSource, UITableViewDelegate>


@end

@implementation WSJFindViewController

- (NSMutableArray *)imageArray {
    if (!_imageArray) {
        _imageArray = [NSMutableArray array];
    }
    return _imageArray;
}
- (NSMutableArray *)videoArray {
    if (!_videoArray) {
        _videoArray = [NSMutableArray array];
    }
    return _videoArray;
}
- (NSMutableArray *)titleArray {
    if (!_titleArray) {
        _titleArray = [NSMutableArray array];
    }
    return _titleArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createTableView];
    
}
- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 113) style:0];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 210 * FITHEIGHT;
    [self.tableView registerClass:[WSVideoTableViewCell class] forCellReuseIdentifier:NSStringFromClass([WSVideoTableViewCell class])];
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        
        [self createData];
    }];
    
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [self createMoreData];
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.imageArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WSVideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WSVideoTableViewCell class])];
    cell.videoModel = self.imageArray[indexPath.row];
    cell.titleModel = self.titleArray[indexPath.row];
    return cell;
}

- (void)getData:(NSString *)url {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray *entitiesArray = responseObject[@"entities"];
        for (NSDictionary *dic in entitiesArray) {
            NSDictionary *cover = dic[@"cover"];
            WSVideoModel *videoModel = [[WSVideoModel alloc] initWithDic:cover];
            [self.imageArray addObject:videoModel];
            
            /** 大字典Model */
            WSTitleModel *titleModel = [[WSTitleModel alloc] initWithDic:dic];
            [self.titleArray addObject:titleModel];
            
            
            /** 视频播放的数据 */
            NSMutableArray *momentCatalogsArray = dic[@"momentCatalogs"];
            NSDictionary *momentCatalogs = [momentCatalogsArray objectAtIndex:0];
            NSMutableArray *allArray = momentCatalogs[@"momentContents"];
            NSDictionary *allDic = [allArray objectAtIndex:0];
            NSDictionary *fileDic = allDic[@"file"];
            WSVideoModel *model = [[WSVideoModel alloc] initWithDic:fileDic];
            [self.videoArray addObject:model];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
    
}

#pragma mark - 上拉刷新 | 下拉加载
- (void)createData {
    self.imageArray = [NSMutableArray array];
    [self getData:URL];
}

- (void)createMoreData {
    static NSInteger i = 2;
    NSString *url = [NSString stringWithFormat:@"http://api.wdquan.cn/moments?limit=10&page=%ld&section=19&cursor=", i];
    i++;
    
    [self getData:url];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    WSPlayVideoViewController *plaVideo = [[WSPlayVideoViewController alloc] init];
    
    WSVideoModel *videoModel = self.videoArray[indexPath.row];
    plaVideo.Mp4Link = [NSString stringWithFormat:@"http://wdquan-space.b0.upaiyun.com%@", videoModel.path];
    WSTitleModel *titleModel = self.titleArray[indexPath.row];
    
    plaVideo.videoTitle = titleModel.Description;
    
    plaVideo.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:plaVideo animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
